package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentImpl implements ITravelCommentService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private TravelCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());
        String refId = travelComment.getRefComment().getId();
        //判断是否是回复评论
        if (StringUtils.hasLength(refId)){
            TravelComment refComment = this.get(refId);
            travelComment.setRefComment(refComment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        repository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        TravelComment travelComment = repository.findById(id).get();
        return travelComment;
    }

    @Override
    public List<TravelComment> list() {
        List<TravelComment> travelComments = repository.findAll();
        return travelComments;
    }

    //攻略评论分页查询
    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query=new Query();
        if (StringUtils.hasLength(qo.getStrategyId)){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId));
        }
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        query.with(pageable);
        List<TravelComment> list = template.find(query, TravelComment.class);
        return DBHelper.query(template,TravelComment.class,query,pageable);
    }

    @Override
    public void update(TravelComment comment) {

    }

    @Override
    public List<TravelComment> queryByTravelId(String id) {
        List<TravelComment> list=repository.findByTravelId(id);
        return list;
    }


}
